<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

class CreateBroadcastPlansTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('broadcast_plans', function (Blueprint $table) {
            $table->comment('广播计划配置表');
            $table->bigIncrements('id')->comment('主键ID');
            $table->string('plan_name', 64)->nullable()->comment('计划名称');
            $table->text('plan_description')->nullable()->comment('计划描述');
            $table->text('content_text')->nullable()->comment('广播文字内容');
            $table->string('content_audio_file', 255)->nullable()->comment('音频文件路径');
            $table->json('target_devices')->nullable()->comment('目标设备（JSON格式：设备ID数组）');
            $table->json('target_groups')->nullable()->comment('目标分组（JSON格式：分组名称数组）');
            $table->time('start_time')->nullable()->comment('开始时间，控制播放时间段');
            $table->time('end_time')->nullable()->comment('结束时间，控制播放时间段');
            $table->string('execution_day', 256)->nullable()->comment('执行日期，逗号间隔，按不同重复类型');
            $table->tinyInteger('repeat_type')->default(1)->comment('重复类型：1-不重复，2-每天，3-每周，4-每月');
            $table->integer('repeat_interval')->nullable()->comment('重复间隔（分钟）');
            $table->integer('repeat_count')->default(1)->comment('重复次数');
            $table->integer('trigger_event')->nullable()->comment('触发事件ID');
            $table->json('trigger_condition')->nullable()->comment('触发条件（JSON格式）');
            $table->tinyInteger('volume_day')->default(70)->comment('音量');
            $table->tinyInteger('plan_status')->default(1)->comment('计划状态：1-草稿，2-待执行，3-执行中，4-已取消');
            $table->timestamp('last_execution_time')->nullable()->comment('最后执行时间');
            $table->integer('execution_count')->default(0)->comment('已执行次数');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();
            
            // 索引
            $table->index('plan_name', 'idx_plan_name');
            $table->index('plan_status', 'idx_plan_status');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('broadcast_plans');
    }
}